package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.core.domain.model.LoginUser;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.service.IAlertDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 报警数据Service业务层处理
 * 
 * @author alexis
 * @date 2025-01-15
 */
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper, AlertData> implements IAlertDataService
{
    @Autowired
    private AlertDataMapper alertDataMapper;

    /**
     * 查询报警数据
     * 
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id)
    {
        return alertDataMapper.selectById(id);
    }

    /**
     * 查询报警数据列表
     * 
     * @param alertData 报警数据
     * @return 报警数据
     */
    @Override
    public List<AlertData> selectAlertDataList(AlertData alertData)
    {
        return alertDataMapper.selectAlertDataList(alertData);
    }

    /**
     * 新增报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData)
    {
        return alertDataMapper.insert(alertData);
    }

    /**
     * 修改报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData)
    {
        return alertDataMapper.updateById(alertData);
    }

    /**
     * 批量删除报警数据
     * 
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids)
    {
        return alertDataMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除报警数据信息
     * 
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id)
    {
        return alertDataMapper.deleteById(id);
    }


    /**
     * 条件分页查询报警数据
     *
     * @param deviceName 设备名称
     * @param startTime  开始时间
     * @param endTime    结束时间
     * @param status     状态
     * @return 报警数据集合
     */
    @Override
    public List<AlertData> selectAlertDataList(String deviceName, LocalDateTime startTime, LocalDateTime endTime, Integer status) {
        // 获取当前登录用户
        LoginUser loginUser = SecurityUtils.getLoginUser();

        // 查询并返回当前登录人的报警数据
        return list(Wrappers.<AlertData>lambdaQuery()
                .eq(AlertData::getUserId, loginUser.getUserId())
                .eq(StringUtils.isNotEmpty(deviceName), AlertData::getDeviceName, deviceName)
                .eq(ObjectUtils.isNotEmpty(status), AlertData::getStatus, status)
                .between(ObjectUtils.isNotEmpty(startTime) && ObjectUtils.isNotEmpty(endTime), AlertData::getCreateTime, startTime, endTime));
    }

    /**
     * 处理报警数据
     *
     * @param alertData 报警数据对象
     */
    @Override
    public void handleAlertData(AlertData alertData) {
        // 获取当前登录人
        LoginUser loginUser = SecurityUtils.getLoginUser();

        // 根据报警数据id获取报警数据对象完整信息
        AlertData alertDataInDb = getById(alertData.getId());

        // 获取和本条报警信息相关的一组报警信息
        List<AlertData> alertDataList = list(Wrappers.<AlertData>lambdaQuery()
                .eq(AlertData::getProductKey, alertDataInDb.getProductKey())
                .eq(AlertData::getIotId, alertDataInDb.getIotId())
                .eq(AlertData::getFunctionId, alertDataInDb.getFunctionId())
                .eq(AlertData::getAlertRuleId, alertDataInDb.getAlertRuleId())
                .eq(AlertData::getCreateTime, alertDataInDb.getCreateTime()));

        // 批量处理报警数据
        alertDataList.forEach(data -> {
            data.setProcessorId(loginUser.getUserId());
            data.setProcessorName(loginUser.getUsername());
            data.setProcessingResult(alertData.getProcessingResult());
            data.setProcessingTime(alertData.getProcessingTime());
            // 设置状态为已处理
            data.setStatus(1);
        });

        updateBatchById(alertDataList);
    }
}
